package com.zh.zhbootauth.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * 描述: Security 核心配置类-资源服务器(@EnableResourceServer)
 *
 * @author: yanglin
 * @Date: 2021-10-29-21:08
 * @Version: 1.0
 */
@Configuration
@EnableWebSecurity
public class MyWebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private MyUserDetailsService userDetailsService;

    @Autowired
    private LoginSuccessHandlerConfig mySuccessHandler;

    @Autowired
    private LoginFailureHandlerConfig myFailHandler;

    @Autowired
    private LogoutHandlerConfig logoutHandlerConfig;

    @Autowired
    private PasswordEncoder passwordEncoder;

    /**
     * 解决AuthenticationManager 无法注入问题
     * @return
     * @throws Exception
     */
    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    /**
     * 请配置这个，以保证在刷新Token时能成功刷新
     * @param auth
     * @throws Exception
     */
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        // 配置密码加密方式  BCryptPasswordEncoder，添加用户加密的时候请也用这个加密
        auth
                //用户认证处理
                .userDetailsService(userDetailsService)
                //密码处理
                .passwordEncoder(passwordEncoder);
    }


    @Override
    public void configure(HttpSecurity http) throws Exception {
        http
                // 关闭csrf防护
                .csrf().disable()
                .headers().frameOptions().disable()
                .and();
        http
                //登录处理
                // .addFilterBefore(captchaFilterConfig, UsernamePasswordAuthenticationFilter.class)
                .formLogin()
                //登录需要经过的url请求
                .loginProcessingUrl("/access/auth/login")
                //未登录时默认跳转页面
                .loginPage("/login")
                .failureHandler(myFailHandler)
                .successHandler(mySuccessHandler)
                .permitAll()
                .and();
        http
                //登出处理
                .logout()
                .addLogoutHandler(logoutHandlerConfig)
                .logoutUrl("/logout")
                .logoutSuccessUrl("/login")
                .permitAll()
                .and();
        http    .requestMatchers().anyRequest().and().authorizeRequests()
                //无需权限访问
                .antMatchers("/oauth/**").permitAll()
                .and();
        http
                //定制url访问权限，动态权限读取，参考：https://www.jianshu.com/p/0a06496e75ea
                // .addFilterAfter(dynamicallyUrlInterceptor(), FilterSecurityInterceptor.class)
                .authorizeRequests()
                //无需权限访问
                .antMatchers("/favicon.ico","/view/**", "/layui/**", "/js/**", "/css/**", "/img/**").permitAll()
                //其他接口需要登录后才能访问
                .anyRequest().authenticated()
                .and();
    }


}
